package ru.osu.acm.common.industrial;

import ru.osu.acm.common.entities.Message;

import java.util.ArrayList;
import java.util.Date;
import java.sql.*;

/**
 * User: aleksey
 * Date: Oct 1, 2009
 * Time: 10:56:52 PM
 */
public class MessageFactory extends Factory {
    public static Message getMessage(int messageId, Connection con, boolean closeConnection) {
        Message contest = null;

        try {
            PreparedStatement statement = con.prepareStatement("select * from message where id = ?");
            statement.setInt(1, messageId);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                contest = new Message(
                        resultSet.getInt("id"),
                        TeamFactory.getTeam(resultSet.getInt("team"), con, false),
                        PersonFactory.getPerson(resultSet.getInt("person"), con, false),
                        ContestFactory.getContest(resultSet.getInt("contest"), con, false),
                        ProblemFactory.getProblem(resultSet.getInt("problem"), con, false),
                        resultSet.getInt("incoming") == 1,
                        resultSet.getString("message").trim(),
                        resultSet.getTimestamp("received"));
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return contest;
    }

    public static ArrayList<Message> getUserMessages(int userId, Connection con, boolean closeConnection) {
        ArrayList<Message> messages = new ArrayList<Message>();

        try {
            PreparedStatement statement = con.prepareStatement("select * from message where person = ? order by received asc");
            statement.setInt(1, userId);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Message message = getMessage(resultSet.getInt("id"), con, false);
                messages.add(message);
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return messages;
    }

    public static ArrayList<Message> getTeamMessages(int teamId, Connection con, boolean closeConnection) {
        ArrayList<Message> messages = new ArrayList<Message>();

        try {
            PreparedStatement statement = con.prepareStatement("select * from message where team = ? order by received asc");
            statement.setInt(1, teamId);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Message message = getMessage(resultSet.getInt("id"), con, false);
                messages.add(message);
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return messages;
    }

    public static int getUserMessageCount(int userId, Connection con, boolean closeConnection) {
        int count = 0;

        try {
            PreparedStatement statement = con.prepareStatement("select count(*) as messagecount from message where person = ?");
            statement.setInt(1, userId);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                count = resultSet.getInt("messagecount");
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return count;
    }

    public static int getTeamMessageCount(int teamId, Connection con, boolean closeConnection) {
        int count = 0;

        try {
            PreparedStatement statement = con.prepareStatement("select count(*) as messagecount from message where team = ?");
            statement.setInt(1, teamId);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                count = resultSet.getInt("messagecount");
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return count;
    }

    public static void updateMessage(Message message, Connection con, boolean closeConnection) {
        if (message.getId() == -1) {
            message.setId(getNextId("message", con, false));

            try {
                PreparedStatement statement = con.prepareStatement("insert into message (id, team, person, contest, problem, incoming, message, received) values (?, ?, ?, ?, ?, ?, ?, ?)");
                statement.setInt(1, message.getId());
                statement.setInt(2, message.getTeamId());
                statement.setInt(3, message.getPersonId());
                statement.setInt(4, message.getContest().getId());
                statement.setInt(5, message.getProblem().getId());
                statement.setInt(6, message.isIncoming() ? 1 : 0);
                statement.setString(7, message.getMessage());
                statement.setTimestamp(8, new Timestamp(message.getReceived().getTime()));
                statement.executeUpdate();
                statement.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }

            closeConnection(con, closeConnection);
            return;
        }

        try {
            PreparedStatement statement = con.prepareStatement("update message set team = ?, person = ?, contest = ?, problem = ?, incoming = ?, message = ?, received = ? where id = ?");
            statement.setInt(1, message.getTeam() == null ? null : message.getTeam().getId());
            statement.setInt(2, message.getPerson() == null ? null : message.getPerson().getId());
            statement.setInt(3, message.getContest().getId());
            statement.setInt(4, message.getProblem().getId());
            statement.setInt(5, message.isIncoming() ? 1 : 0);
            statement.setString(6, message.getMessage());
            statement.setTimestamp(7, new Timestamp(message.getReceived().getTime()));
            statement.setInt(8, message.getId());
            statement.executeUpdate();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
    }
}
